unit RelatorioCRporCliente;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, UFormRelatorio, DB, DBTables, QRCtrls, QuickRpt, ExtCtrls,
  StdCtrls;

type
  TFRelatorioCRCliente = class(TFormRelatorio)
    QRGroup1: TQRGroup;
    QRDBText1: TQRDBText;
    QRLabel5: TQRLabel;
    QRLabel6: TQRLabel;
    QRLabel8: TQRLabel;
    rlTitulocampoData: TQRLabel;
    QRLabel10: TQRLabel;
    QRLabel11: TQRLabel;
    QRLabel12: TQRLabel;
    QRLabel13: TQRLabel;
    QRBand4: TQRBand;
    QRShape5: TQRShape;
    QRShape6: TQRShape;
    QRShape7: TQRShape;
    QRExpr5: TQRExpr;
    QRExpr6: TQRExpr;
    QRShape2: TQRShape;
    QRShape3: TQRShape;
    QRShape4: TQRShape;
    QRLabel14: TQRLabel;
    QRLabel15: TQRLabel;
    QRBand2: TQRBand;
    QRDBText2: TQRDBText;
    QRDBText4: TQRDBText;
    QRDBText5: TQRDBText;
    QRLHistorico1: TQRDBText;
    QRDBTvalor: TQRDBText;
    QRDBTpago: TQRDBText;
    QRDBTsaldo: TQRDBText;
    QRExpr7: TQRExpr;
    QRLSituacao: TQRLabel;
    QRLHistorico2: TQRDBText;
    QRLVencimento: TQRLabel;
    QRLDataVencimento: TQRLabel;
    QRLDocumento: TQRLabel;
    QRLDataDocumento: TQRLabel;
    QRLCentro: TQRLabel;
    QRLabel4: TQRLabel;
    QRLGrCCusto: TQRLabel;
    QRLabel16: TQRLabel;
    QRLabel7: TQRLabel;
    QRDBText3: TQRDBText;
    //QRLabel3: TQRLabel;
    qrlTotalDescontado: TQRLabel;
    QRLabel17: TQRLabel;
    QRLabel18: TQRLabel;
    qrTotalSaldo: TQRLabel;
    QRLabel20: TQRLabel;
    QRDBText6: TQRDBText;
    QRLabel21: TQRLabel;
    QRLabel22: TQRLabel;
    QRLabel9: TQRLabel;
    QRLDataPagto: TQRLabel;
    QRLabel24: TQRLabel;
    QRDBText7: TQRDBText;
    qrTotalDescto: TQRLabel;
    QRExpr1: TQRExpr;
    QRLabel19: TQRLabel;
    procedure QRBand2BeforePrint(Sender: TQRCustomBand;
      var PrintBand: Boolean);
    procedure FormCreate(Sender: TObject);
    procedure QRSysData3Print(sender: TObject; var Value: String);
    procedure QuickRep1BeforePrint(Sender: TCustomQuickRep;
      var PrintReport: Boolean);
    procedure QRExpr3Print(sender: TObject; var Value: String);
    procedure QRExpr6Print(sender: TObject; var Value: String);
    procedure QRLabel16Print(sender: TObject; var Value: String);
    procedure QRBEmpBeforePrint(Sender: TQRCustomBand;
      var PrintBand: Boolean);
    procedure QRBEmpAfterPrint(Sender: TQRCustomBand;
      BandPrinted: Boolean);
    procedure QRDBTsaldoPrint(sender: TObject; var Value: String);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FRelatorioCRCliente: TFRelatorioCRCliente;
  desconto : real = 0.0;
  descPorCli:real = 0.0;
  Valor, Valor_Doc, Valor_Pago, Valor_descto : Real;
  
implementation

uses UGeral, PesquisaCRporVencimento;

{$R *.dfm}

procedure TFRelatorioCRCliente.QRBand2BeforePrint(Sender: TQRCustomBand;
  var PrintBand: Boolean);
begin
  inherited;

  Valor        := Valor        + FPesquisaContasReceber.QPesquisa.Fieldbyname('VALOR').AsFloat;
  Valor_Doc    := Valor_Doc    + FPesquisaContasReceber.QPesquisa.Fieldbyname('CR_VALORDOCTO').AsFloat;
  Valor_Pago   := Valor_Pago   + FPesquisaContasReceber.QPesquisa.Fieldbyname(QRDBTpago.DataField).AsFloat;
  Valor_descto := Valor_descto + FPesquisaContasReceber.QPesquisa.Fieldbyname('DESCTO').AsFloat;

  if (FPesquisaContasReceber.QPesquisa.fieldByname('HISTORICO2').AsString <> '') or
     (FPesquisaContasReceber.QPesquisa.fieldByname('DESCTO').AsFloat > 0) then
  begin
    QRBand2.Height:=QRLHistorico2.Top + QRLHistorico2.Height;
    //QRDBTvalor.Top:=QRLHistorico2.Top;
    //QRDBTpago.Top:=QRLHistorico2.Top;
    //QRDBTsaldo.Top:=QRLHistorico2.Top;
  end
  else
  begin
    QRBand2.Height:=QRLHistorico2.Top;
    //QRDBTvalor.Top:=QRLHistorico1.Top;
    //QRDBTpago.Top:=QRLHistorico1.Top;
    //QRDBTsaldo.Top:=QRLHistorico1.Top;
  end;

  if FPesquisaContasReceber.QPesquisa.FieldByName('PORT_DESCONTADO').AsInteger = 1 then
  begin
    desconto := desconto   + FPesquisaContasReceber.QPesquisa.FieldByName('VALOR').asfloat -
                             (FPesquisaContasReceber.QPesquisa.FieldByName('PAGO').asfloat + FPesquisaContasReceber.QPesquisa.FieldByName('DESCTO').asfloat);
    descPorCli:=descPorCli + FPesquisaContasReceber.QPesquisa.FieldByName('VALOR').asfloat -
                             (FPesquisaContasReceber.QPesquisa.FieldByName('PAGO').asfloat + FPesquisaContasReceber.QPesquisa.FieldByName('DESCTO').asfloat);
  end;

  if FPesquisaContasReceber.CBOmitirDethalhes.Checked then
    PrintBand := False
  else
    PrintBand := true;

end;

procedure TFRelatorioCRCliente.FormCreate(Sender: TObject);
begin
  inherited;
 QRelatorio.Close;
 QRelatorio.SQL.Clear;
 QRelatorio.SQL.Add('Select Emp_Razao as Razao from empresa ');
 QRelatorio.SQL.Add('Where Emp_Codigo = :Cod ');
 QRelatorio.ParamByName('Cod').AsInteger:=Codigoempresa;
 QRelatorio.Open;
 QRLNomeEmpresa.Caption:=QRelatorio.fieldByname('Razao').AsString;

end;

procedure TFRelatorioCRCliente.QRSysData3Print(sender: TObject;
  var Value: String);
begin
  inherited;
value:='';
end;

procedure TFRelatorioCRCliente.QuickRep1BeforePrint(
  Sender: TCustomQuickRep; var PrintReport: Boolean);
begin
  inherited;
  if QRLGrCCusto.Caption = QRLGrCCusto.Name then
    QRLGrCCusto.Caption := '';
end;

procedure TFRelatorioCRCliente.QRExpr3Print(sender: TObject;
  var Value: String);
begin
  inherited;
 // Value := FormatFloat('#,##0.00',QRExpr3.Value.dblResult - desconto);
end;

procedure TFRelatorioCRCliente.QRExpr6Print(sender: TObject;
  var Value: String);
begin
  inherited;
  Value := FormatFloat('#,##0.00',QRExpr6.Value.dblResult - descPorCli);
  descPorCli := 0.0;
end;

procedure TFRelatorioCRCliente.QRLabel16Print(sender: TObject;
  var Value: String);
begin
  inherited;
  if FPesquisaContasReceber.QPesquisa.FieldByName('PORT_DESCONTADO').AsInteger = 1 then
    value := 'D'
  else
    value := '';
end;

procedure TFRelatorioCRCliente.QRBEmpBeforePrint(Sender: TQRCustomBand;
  var PrintBand: Boolean);
begin
  inherited;
  qrlTotalDescontado.Caption := FormatFloat('#,##0.00;; ',desconto);
  desconto := 0.0;

  QRLabel19.Caption     := FormatFloat('0.,00',Valor_Doc);
  QRLabel17.Caption     := FormatFloat('0.,00',Valor);
  QRLabel18.Caption     := FormatFloat('0.,00',Valor_Pago);
  qrTotalDescto.Caption := FormatFloat('#,##0.00;; ',Valor_descto);
  qrTotalSaldo.Caption  := FormatFloat('0.,00',Valor - (Valor_Pago + Valor_descto));

end;

procedure TFRelatorioCRCliente.QRBEmpAfterPrint(Sender: TQRCustomBand;
  BandPrinted: Boolean);
begin
  inherited;
  Valor        := 0;
  Valor_Doc    := 0;
  Valor_Pago   := 0;
  Valor_descto := 0;
end;

procedure TFRelatorioCRCliente.QRDBTsaldoPrint(sender: TObject;
  var Value: String);
begin
  inherited;
  Value := FormatFloat('#,##0.00',QRDBTsaldo.DataSet.FieldByName('VALOR').AsFloat -
                                  (QRDBTsaldo.DataSet.FieldByName('PAGO').AsFloat + QRDBTsaldo.DataSet.FieldByName('DESCTO').AsFloat));

end;

end.
